package com.example.backend.controller;

import com.example.backend.entity.Customer;
import com.example.backend.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @PostMapping("/save")
    public Customer savecustomer(@RequestBody Customer customer){
        return customerService.save(customer);
    }
    @GetMapping("/list")
    public List<Customer> findAll(){
        return customerService.findAll();
    }
    @GetMapping("/search")
    public List<Customer> findcustomerByName(@Param("key") String key, @Param("value") String value){
        switch (key){
            case "id":return customerService.findById(Integer.parseInt(value));
            case "name":return customerService.findByName(value);
            case "identity":return customerService.findByIdentity(value);
            case "age":return customerService.findByAge(Integer.parseInt(value));
            case "address":return customerService.findByAddress(value);
            case "phone":return customerService.findByPhone(value);
        }
        return null;
    }
    @GetMapping("/repeat")
    public boolean findName(@Param("name")String name){
        return customerService.findName(name);
    }
    @GetMapping("/changeRepeat")
    public boolean findNameAndId(@Param("id") Integer id,@Param("name")String name){
        System.out.println(name);
        return customerService.findNameAndId(id,name);
    }
    @PostMapping("/update")
    public Customer updatecustomer(@RequestBody Customer customer){
        return customerService.update(customer);
    }
    @GetMapping("/delete")
    public void deletecustomer(@Param("id")Integer id){
        customerService.delete(id);
    }
}
